package smartbooks.ui.options;

import java.awt.AWTEvent;
import java.awt.Toolkit;
import smartbooks.ui.listeners.CommonMouseListener;
import smartbooks.ui.util.CommonFunctions;

/**
 *
 * @author Ankit
 */
public class TaxRateDialog extends javax.swing.JDialog {

    /**
     * Creates new form TaxRateDialog
     * @param parent
     * @param modal
     */
    public TaxRateDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        /*
         * This is to be included so that the Exception(BalloonTip) is hidden 
         * whenever user clicks somewhere on the screen or presses a key.
         */
        Toolkit.getDefaultToolkit().addAWTEventListener(
            new CommonMouseListener(this), AWTEvent.MOUSE_EVENT_MASK);        
        Toolkit.getDefaultToolkit().addAWTEventListener(
            new CommonMouseListener(this), AWTEvent.KEY_EVENT_MASK);
        
        this.setLocationRelativeTo(parent);
        //this.setVisible(true);
    }
    
    private void changeTaxRateSelection() {
        simpleTaxRateTextField.setEnabled(simpleTaxRateRadioButton.isSelected());
        
        taxRate1ComboBox.setEnabled(!simpleTaxRateRadioButton.isSelected());
        taxRate2ComboBox.setEnabled(!simpleTaxRateRadioButton.isSelected());
        compoundingCheckBox.setEnabled(!simpleTaxRateRadioButton.isSelected());
    }
    
    public void showDialog(){
        setVisible(true); // when this is called the dialog will show up
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        taxRateTypeButtonGroup = new javax.swing.ButtonGroup();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        taxNameTextField = new javax.swing.JTextField();
        simpleTaxRateRadioButton = new javax.swing.JRadioButton();
        simpleTaxRateTextField = new javax.swing.JTextField();
        combinedTaxRateRadioButton = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        taxRate1ComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        taxRate2ComboBox = new javax.swing.JComboBox();
        compoundingCheckBox = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();
        createOrSaveTaxRateButton = new javax.swing.JButton();
        clearTaxRateButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tax Rate");
        setModal(true);
        setResizable(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("<html><font color=red>*</font>Tax Name:</html>");

        taxNameTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        taxRateTypeButtonGroup.add(simpleTaxRateRadioButton);
        simpleTaxRateRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        simpleTaxRateRadioButton.setSelected(true);
        simpleTaxRateRadioButton.setText("Simple Tax Rate (%):");
        simpleTaxRateRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                simpleTaxRateRadioButtonItemStateChanged(evt);
            }
        });

        simpleTaxRateTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        simpleTaxRateTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                simpleTaxRateTextFieldKeyTyped(evt);
            }
        });

        taxRateTypeButtonGroup.add(combinedTaxRateRadioButton);
        combinedTaxRateRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        combinedTaxRateRadioButton.setText("Combined Tax Rate:");
        combinedTaxRateRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                combinedTaxRateRadioButtonItemStateChanged(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Tax Rate 1 (%):");

        taxRate1ComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        taxRate1ComboBox.setEnabled(false);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Tax Rate 2 (%):");

        taxRate2ComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        taxRate2ComboBox.setEnabled(false);

        compoundingCheckBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        compoundingCheckBox.setText("Compounding so Rate 2 is applied after Rate 1");
        compoundingCheckBox.setEnabled(false);

        createOrSaveTaxRateButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        createOrSaveTaxRateButton.setText("Create");

        clearTaxRateButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        clearTaxRateButton.setText("Clear");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(simpleTaxRateRadioButton)
                                .addComponent(combinedTaxRateRadioButton)
                                .addGroup(jPanel4Layout.createSequentialGroup()
                                    .addGap(21, 21, 21)
                                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel5))))
                            .addGap(4, 4, 4)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                .addComponent(simpleTaxRateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(taxRate1ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(taxRate2ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addComponent(compoundingCheckBox)))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(81, 81, 81)
                            .addComponent(taxNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                                .addGap(185, 185, 185)
                                .addComponent(clearTaxRateButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createOrSaveTaxRateButton)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taxNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(simpleTaxRateRadioButton)
                    .addComponent(simpleTaxRateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(combinedTaxRateRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(taxRate1ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel6)
                    .addComponent(taxRate2ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(compoundingCheckBox)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createOrSaveTaxRateButton)
                    .addComponent(clearTaxRateButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void simpleTaxRateRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_simpleTaxRateRadioButtonItemStateChanged
        changeTaxRateSelection();
    }//GEN-LAST:event_simpleTaxRateRadioButtonItemStateChanged

    private void simpleTaxRateTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_simpleTaxRateTextFieldKeyTyped
        CommonFunctions.addFloatPatternKeyTypedListener(evt);
    }//GEN-LAST:event_simpleTaxRateTextFieldKeyTyped

    private void combinedTaxRateRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_combinedTaxRateRadioButtonItemStateChanged
        changeTaxRateSelection();
    }//GEN-LAST:event_combinedTaxRateRadioButtonItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TaxRateDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TaxRateDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TaxRateDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TaxRateDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TaxRateDialog dialog = new TaxRateDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearTaxRateButton;
    private javax.swing.JRadioButton combinedTaxRateRadioButton;
    private javax.swing.JCheckBox compoundingCheckBox;
    private javax.swing.JButton createOrSaveTaxRateButton;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JRadioButton simpleTaxRateRadioButton;
    private javax.swing.JTextField simpleTaxRateTextField;
    private javax.swing.JTextField taxNameTextField;
    private javax.swing.JComboBox taxRate1ComboBox;
    private javax.swing.JComboBox taxRate2ComboBox;
    private javax.swing.ButtonGroup taxRateTypeButtonGroup;
    // End of variables declaration//GEN-END:variables
}
